package com.baidu.infinity.ui.util

import android.os.Build
import androidx.annotation.RequiresApi
import com.baidu.infinity.model.LayerModel
import com.baidu.infinity.ui.util.state.LayerState

class LayerModelManager private constructor(){
    val dataList : ArrayList<LayerModel> = arrayListOf()
    var currentSelectLayer: LayerModel ?= null
    companion object{
        val instance = LayerModelManager()
    }

    fun getLayerData(): List<LayerModel>{
        return dataList
    }
    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    fun addLayer(layerModel: LayerModel){
        dataList.addFirst(layerModel)
        if(currentSelectLayer != null){
            currentSelectLayer!!.state = LayerState.NORMAL
        }
        currentSelectLayer = layerModel

    }
    fun selectLayer(layerModel: LayerModel){
        currentSelectLayer!!.state = LayerState.NORMAL
        layerModel.state = LayerState.SELECTED
        currentSelectLayer = layerModel
    }
    fun getCurrentSelectLayerId(): Int?{
        if (currentSelectLayer == null){
            return null
        }else{
            return currentSelectLayer!!.id
        }
    }
    fun removeLayer(index: Int){
        if(dataList.indexOf(currentSelectLayer!!) != -1 || index < 0 || dataList.size - 1 < 0)return
        if(dataList.size == index){
            selectLayer(dataList[dataList.size - 1])
        }else{
            selectLayer(dataList[index])
        }
    }

}